package com.woniuxy.driverschool.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.woniuxy.driverschool.entity.Message4D2admin;
import com.woniuxy.driverschool.entity.Period;
import com.woniuxy.driverschool.entity.ResponseResult;
import com.woniuxy.driverschool.service.PeriodService;
import com.woniuxy.driverschool.service.courseArrangement.ScheduleLessonsForStu;
import com.woniuxy.driverschool.service.courseArrangement.SelectClassTimeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author shichen
 * @create 2021-10-08-9:52
 * @description
 */
@Controller
public class ScheduleLessonController {

    private static final Log log = LogFactory.get();

    @Autowired
    SelectClassTimeService SelectClassTime;

    @Autowired
    ScheduleLessonsForStu ScheduleLessonsForStu;

    @Autowired
    PeriodService periodService;

    /**
     * 查找具体的教案
     *
     * @param jsonstr
     * @return
     */
    @ResponseBody
    @PostMapping("/selectClass")
    ResponseResult selectClass(@RequestBody String jsonstr){

        log.debug("传过来的JSON串是:  {} ", jsonstr);

        // hutool 工具类来解析JSON串
        JSONObject jsonObject = JSONUtil.parseObj(jsonstr);

        //科目一还是科目二
        String subject = (String) jsonObject.get("value0");
        log.debug("request subject : {} ", subject);

        //教练的名字
        String teacher = (String) jsonObject.get("value1");
        log.debug("request coach ：  {} ", teacher);

        // 选择的排课日期
        String datestr = (String) jsonObject.get("value2");
        log.debug("request dateStr ：  {} ", datestr);


        Date date = DateUtil.parse(datestr, "yyyy-MM-dd");
        // 用来调整时间 ，Spring中 yml 设置时区失败，所以在这里手动调整
        DateTime newDate = DateUtil.offsetDay(date, 1);
        //获得月份，从0开始计数
        int month = DateUtil.month(date);
        //排课方案
        //String id = (String)jsonObject.get("value3");
        String id = null;
        List<Period> allPeriods = periodService.getAllPeriods();
        for (Period period:allPeriods) {
            if(period.getDateFrom().before(date)&&period.getDateTo().after(date)){
                id=period.getPeriodId()+"";
            }
        }




        //将解析的数据传给 service 层
        Map<String, Object> message = SelectClassTime.getMessage(subject, teacher, datestr, id);


        ResponseResult responseResult=new ResponseResult();
        responseResult.setData(message);
        responseResult.setMessage("查找成功！");
        responseResult.setState(200);
        return responseResult;
    }


    /**
     * 查找所有的周期
     * @return
     */
    @RequiresPermissions("user:add")
    @ResponseBody
    @PostMapping("/selectPeriod")
    ResponseResult selectPeriod() {

        System.out.println("");

        JSONArray jsonArray = SelectClassTime.SelectAllPeriod();
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(jsonArray);
        responseResult.setMessage("查找成功");
        responseResult.setState(200);
        return responseResult;
    }

    /**
     * 设置课程
     *
     * @param jsonstr
     * @return
     */
    @ResponseBody
    @PostMapping("/setLesson")
    ResponseResult setLesson(@RequestBody String jsonstr) {

        log.debug("传过来的JSON串是:  {} ", jsonstr);

        // hutool 工具类来解析JSON串
        JSONObject jsonObject = JSONUtil.parseObj(jsonstr);

        // subject
        String subject = (String) jsonObject.get("subject");
        log.debug("request subject : {} ", subject);

        //教练的id
        String coachId = (String) jsonObject.get("coachId");
        log.debug("request coachId : {} ", coachId);

        //periodDetail主键
        String periodDetailId = (String) jsonObject.get("id");
        log.debug("request periodDetail Id ：  {} ", periodDetailId);

        // 选择的排课日期
        String datestr = (String) jsonObject.get("date");
        log.debug("request dateStr ：  {} ", datestr);


        // 响应对象
        ResponseResult responseResult = new ResponseResult();

        //将解析的数据传给 service 层
        try {
            SelectClassTime.setLesson(subject, coachId, datestr, periodDetailId);
            responseResult.setMessage("插入成功");
            responseResult.setState(200);
        } catch (Exception e) {
            log.error("service层setLesson报错：{}", e.getMessage());
            responseResult.setMessage("已经有数据了");
            responseResult.setState(100);
        }


        return responseResult;
    }

    @ResponseBody
    @PostMapping("/removeLesson")
    ResponseResult removeLesson(@RequestBody String jsonstr) {

        log.debug("传过来的JSON串是:  {} ", jsonstr);

        // hutool 工具类来解析JSON串
        JSONObject jsonObject = JSONUtil.parseObj(jsonstr);

        // subject
        String subject = (String) jsonObject.get("subject");
        log.debug("request subject : {} ", subject);

        //教练的id
        String coachId = (String) jsonObject.get("coachId");
        log.debug("request coachId : {} ", coachId);

        //periodDetail主键
        String periodDetailId = (String) jsonObject.get("id");
        log.debug("request periodDetail Id ：  {} ", periodDetailId);

        // 选择的排课日期
        String datestr = (String) jsonObject.get("date");
        log.debug("request dateStr ：  {} ", datestr);


        // 响应对象
        ResponseResult responseResult = new ResponseResult();

        //将解析的数据传给 service 层
        try {
            SelectClassTime.removeLesson(subject, coachId, datestr, periodDetailId);
            responseResult.setMessage("删除成功");
            responseResult.setState(200);
        } catch (Exception e) {
            log.error("service层removeLesson报错：{}", e.getMessage());
            responseResult.setMessage("删除出错");
            responseResult.setState(100);
        }


        return responseResult;
    }

    /**
     * 学员页面查找可选择的课程
     * @param jsonstr
     * @return
     */
    @ResponseBody
    @PostMapping("/selectLesson")
    ResponseResult selectLesson(@RequestBody String jsonstr) {

        log.debug("传过来的JSON串是:  {} ", jsonstr);

        // hutool 工具类来解析JSON串
        JSONObject jsonObject = JSONUtil.parseObj(jsonstr);

        //科目一还是科目二
        String subject = (String) jsonObject.get("value0");
        log.debug("request subject : {} ", subject);

        //教练的名字
        String teacher = (String) jsonObject.get("value1");
        log.debug("request coach ：  {} ", teacher);

        // 选择的排课日期
        String datestr = (String) jsonObject.get("value2");
        log.debug("request dateStr ：  {} ", datestr);


        Date date = DateUtil.parse(datestr, "yyyy-MM-dd");
        // 用来调整时间 ，Spring中 yml 设置时区失败，所以在这里手动调整
        DateTime newDate = DateUtil.offsetDay(date, 1);

        //获得月份，从0开始计数
     //   int month = DateUtil.month(date);
        //排课方案
        //String id = (String)jsonObject.get("value3");
        String id = null;
        List<Period> allPeriods = periodService.getAllPeriods();
        for (Period period:allPeriods) {
            if(period.getDateFrom().before(date)&&period.getDateTo().after(date)){
                id=period.getPeriodId()+"";
            }
        }




        //将解析的数据传给 service 层
        Map<String, Object> message = SelectClassTime.getMessage4Stu(subject, teacher, newDate, id);


        ResponseResult responseResult=new ResponseResult();
        responseResult.setData(message);
        responseResult.setMessage("查找成功！");
        responseResult.setState(200);
        return responseResult;
    }

    @ResponseBody
    @PostMapping("/selectStudent")
    ResponseResult selectStudent(@RequestBody String jsonstr) {

        log.debug("传过来的JSON串是:  {} ", jsonstr);

        // hutool 工具类来解析JSON串
        JSONObject jsonObject = JSONUtil.parseObj(jsonstr);

        //学生名字
        String stuName = (String) jsonObject.get("value0");
        log.debug("request studentName : {} ", stuName);

        //将解析的数据传给 service 层
        List message = ScheduleLessonsForStu.selectStudent(stuName);

        ResponseResult responseResult=new ResponseResult();
        responseResult.setData(message);
        responseResult.setMessage("查找学生成功！");
        responseResult.setState(200);
        return responseResult;
    }

    @ResponseBody
    @PostMapping("/studentChoiceLesson")
    public Message4D2admin studentChoiceLesson(@RequestBody String jsonstr) {

        log.debug("传过来的JSON串是:  {} ", jsonstr);

        // hutool 工具类来解析JSON串
        JSONObject jsonObject = JSONUtil.parseObj(jsonstr);

        //学生名字
        String coachName = (String) jsonObject.get("coachName");
        Integer stuId = (Integer) jsonObject.get("stuId");
        String subject = (String) jsonObject.get("subject");
        Integer tid = (Integer) jsonObject.get("tid");

        Message4D2admin<Object> responseResult = new Message4D2admin<>();
        //将解析的数据传给 service 层
        try {
            SelectClassTime.addAttendance(tid, stuId, Integer.parseInt(subject), coachName);
            responseResult.setCode(0);
            responseResult.setMsg("设置课程成功！");

        } catch (Exception e) {
            // 服务未实现
            responseResult.setCode(501);
            responseResult.setMsg(e.getMessage());
            log.error(e);
        }

        log.debug("发出的消息是{}",responseResult);
        return responseResult;
    }




}
